package com.office.supplies.controller;

import com.github.pagehelper.PageInfo;
import com.office.supplies.pojo.Goods;
import com.office.supplies.pojo.Result;
import com.office.supplies.pojo.dto.AddGoodsDTO;
import com.office.supplies.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author admin
 * @Description 商品相关接口
 * @Date 2023/12/11/0:57
 */
@RestController
@Slf4j
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新增商品
     *
     * @param addGoodsDTO AddGoodsDTO
     * @return Result<String>
     */
    @PostMapping
    public Result<String> insertGoods(@RequestBody AddGoodsDTO addGoodsDTO){
        log.info("新增商品:{}",addGoodsDTO);
        goodsService.insertGoods(addGoodsDTO);
        return Result.success("新增商品成功！");
    }

    /**
     * 根据id删除商品
     *
     * @param id Long
     * @return Result<String>
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteGoods(@PathVariable Long id){
        log.info("删除商品，id:{}",id);
        goodsService.deleteGoodsById(id);
        return Result.success("删除商品成功！");
    }

    /**
     * 修改商品
     *
     * @param goods Goods
     * @return Result<String>
     */
    @PutMapping
    public Result<String> updateGoods(@RequestBody Goods goods){
        log.info("修改商品：{}",goods);
        goodsService.updateGoods(goods);
        return Result.success("修改商品成功！");
    }

    /**
     * 查询商品
     *
     * @param pageNum Integer
     * @param pageSize Integer
     * @param addGoodsDTO AddGoodsDTO
     * @return Result<PageInfo<Goods>>
     */
    @PostMapping("/list/{pageNum}/{pageSize}")
    public Result<PageInfo<Goods>> getGoodsList(@PathVariable("pageNum") Integer pageNum,
                                                @PathVariable("pageSize") Integer pageSize,
                                                @RequestBody AddGoodsDTO addGoodsDTO){
        log.info("查询商品：{}，{}，{}",pageNum,pageSize,addGoodsDTO);
        return Result.success(goodsService.getAllGoodsByPageAndCondtion(pageNum,pageSize,addGoodsDTO));
    }

    /**
     * 查询商品
     *
     * @return Result<List<Goods>>
     */
    @GetMapping("/list")
    public Result<List<Goods>> getGoodsListForBuy(){
        log.info("查询全部商品");
        return Result.success(goodsService.getGoodsList());
    }

}
